package com.gagakuai.web.encript.utils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;


/*
 * @program: hx-decript-springboot-starter
 * @description: 对称加密工具类  MD5值后16位为key,AES采用128位ECB模式,填充模式pkcs5,加密之后再base64转码
 * @author: houhong
 * @create: 2022-06-30 10:28
 */
public class AESUtils {


    public static final String ALGORITHM = "AES";

    /*
     * 生成密钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator secretGenerator = KeyGenerator.getInstance(ALGORITHM);
        SecureRandom secureRandom = new SecureRandom();
        secretGenerator.init(secureRandom);
        SecretKey secretKey = secretGenerator.generateKey();
        return secretKey;
    }

    static Charset charset = Charset.forName("UTF-8");

    /*
     * 加密
     *
     * @param content
     * @param secretKey
     * @return
     */
    public static byte[] encrypt(String content, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 加密
        return aes(content.getBytes(charset), Cipher.ENCRYPT_MODE, secretKey);
    }

    /*
     * 解密
     *
     * @param contentArray
     * @param secretKey
     * @return
     */
    public static String decrypt(byte[] contentArray, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 解密
        byte[] result = aes(contentArray, Cipher.DECRYPT_MODE, secretKey);
        return new String(result, charset);
    }

    private static byte[] aes(byte[] contentArray, int mode, SecretKey secretKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(mode, secretKey);
        byte[] result = cipher.doFinal(contentArray);
        return result;
    }

    public static void main(String[] args) {
        String content = "青年人需要认识到自己所负的责任！";
        SecretKey secretKey;
        try {
            long timeStartEncry = System.currentTimeMillis();
            // 生成密钥
            secretKey = generateKey();

            byte[] encryptResult = encrypt(content, secretKey);
            long timeEndEncry = System.currentTimeMillis();
            System.out.println("加密后的结果为==" + new String(encryptResult, charset));

            String decryptResult = decrypt(encryptResult, secretKey);
            System.out.println("解密后的结果==" + decryptResult);

            System.out.println("加密用时：" + (timeEndEncry - timeStartEncry));
        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
    }
}
